package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopsAdapter;
import com.iexbuy.ihg.adapter.YunGuoItemListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.GoodCategoryObject;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.ShopCenter;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopListResponse;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.entity.YunItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class ShopCenterZeroGouSearchResultFragment extends BaseFragment {

	private static ShopCenterZeroGouSearchResultFragment homeFragmentInstent;


	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.categorySelectLayout)
	private LinearLayout categorySelectLayout;
	@ViewInject(R.id.categoryLayout)
	private LinearLayout categoryLayout;
	@ViewInject(R.id.categoryListView)
	private ListView categoryListView;
	@ViewInject(R.id.categoryBtn)
	private Button categoryBtn;
	@ViewInject(R.id.searchfailLayout)
	private RelativeLayout searchfailLayout;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.adItemList)

	private List<YunItemListObject> yunItemListObjects = new ArrayList<YunItemListObject>();

	private int pageNo = 0;
	private boolean hasNextPage;
	private YunGuoItemListAdapter yunGuoItemListAdapter;
	private List<Shop> shops = new ArrayList<Shop>();
	private static String category;

	private static ShopCenter shopCenter;
	//云购





	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
								 Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_shopcenter_zerogou_search_result, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.yun_guo_list_title));

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();
	}

	//刷新整个页面数据
	private void refreshDate() {
		pageNo = 0;
		initZeroGouListDate();
	}

	/**
	 * 查询列表
	 */
	private void initZeroGouListDate() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		paramMap.put("areaCode", shopCenter.getArea());

		if (category != null) {
			paramMap.put("category", category);
		}

		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_READY_OPEN_ITEMS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						YunItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, YunItemListObjectResponse.class);
						if (pageNo == 0) {
							yunItemListObjects.clear();
						}
						yunItemListObjects.addAll(rsp.getBody());

						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}


	private void initListView() {
		if (yunGuoItemListAdapter == null) {
			yunGuoItemListAdapter = new YunGuoItemListAdapter(mBaseActivity, yunItemListObjects);
			listView.setAdapter(yunGuoItemListAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					YunGuoItemDetailFragment.setYunItemListObject(yunItemListObjects.get(i));

					CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoItemDetailFragment.class);
				}
			});

		}
	}

	private void loadMore() {
		pageNo ++;
		initZeroGouListDate();
	}




	public static void setShopCenter(ShopCenter shopCenter) {
		ShopCenterZeroGouSearchResultFragment.shopCenter = shopCenter;
	}
	public static void setCategory(String category) {
		ShopCenterZeroGouSearchResultFragment.category = category;
	}


	/**
	 * @return the homeFragmentInstent
	 */
	public static ShopCenterZeroGouSearchResultFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
